package com.jz.beijianbao.planclass.controller;


import com.jz.beijianbao.planclass.bean.StatusVo;
import com.jz.beijianbao.planclass.bean.PlanClass;
import com.jz.beijianbao.planclass.bean.PlanClassAdd;
import com.jz.beijianbao.planclass.bean.PlanClassSelectVo;
import com.jz.beijianbao.planclass.enumclass.StatusEnum;
import com.jz.beijianbao.planclass.service.PlanService;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@RestController
public class TestPlan {

    @Autowired
    private PlanService planService;

    @GetMapping("queryPlanXiaLa")
    public ResultVO queryXiaLa(){
        List<StatusVo> statusVos = new ArrayList<>();
        for (StatusEnum statusEnum:StatusEnum.values()) {
            StatusVo statusVo = new StatusVo(statusEnum.getStatus(),statusEnum.getStatusName());
            statusVos.add(statusVo);
        }
        return ResultBuildVO.success(statusVos);
    }

//  分页查询
    @PostMapping("queryPlanPage")
    public ResultVO queryPage(@RequestBody PlanClassSelectVo planClassSelectVo){
        ResultVO resultVO = planService.queryPag(planClassSelectVo);
        return resultVO;

    }
//    新增大件
    @PostMapping("addPlan")
    public ResultVO add(@RequestBody  @Valid PlanClassAdd planClassAdd, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVO resultVO = planService.add(planClassAdd);
        return resultVO;

    }
    //回显
    @PostMapping("echoPlan")
    public ResultVO echo(Integer id){
        ResultVO resultVO = planService.selectById(id);
        return resultVO;
    }
//    回显后修改
    @PostMapping("updatePlan")
    public ResultVO update(@RequestBody  @Valid PlanClassAdd planClassAdd, BindingResult bindingResult){
        ResultVO resultVO = planService.update(planClassAdd);
        return resultVO;
    }
//    修改状态
    @PostMapping("statusPlan")
    public ResultVO status(@RequestBody PlanClass planClass){
        ResultVO resultVO =planService.status(planClass);
        return resultVO;
    }















}
